import {
  setUserToken,
  resetUser
} from "./user";
import {
  reqLogin
} from "@/api/login";
import {
  setToken,
  removeToken
} from "@/utils/auth";


export const userLogin = (data) => (dispatch) => {
  return new Promise((resolve, reject) => {
    reqLogin(data)
      .then((response) => {
        const {
          data
        } = response;
        if (response.code === 200) {
          const token = data.token;
          dispatch(setUserToken(token));
          setToken(token);
          resolve(response);
        } else {

          reject(response.msg);
        }
      })
      .catch((error) => {
        console.log(error);
        reject(error);
      });
  });
};

export const logout = (token) => (dispatch) => {
  return new Promise((resolve, reject) => {
    //   reqLogout(token)
    //     .then((response) => {
    //       const {
    //         data
    //       } = response;
    //       if (data.status === 0) {
    //         dispatch(resetUser());
    //         removeToken();
    //         resolve(data);
    //       } else {
    //         const msg = data.message;
    //         reject(msg);
    //       }
    //     })
    //     .catch((error) => {
    //       reject(error);
    //     });
    // });
    dispatch(resetUser());
    removeToken();
    resolve();
  })
};